

public class Multime implements Structura {
	protected int[] multime;
	private static int poz;//poz. curenta in multime
	private static int lungimeMaxima;// lung. max. a multimii
	public Multime()
	{
		poz=0;
		lungimeMaxima=10;
		multime=new int[10];
	}//constructor de initializare
	public Multime(int dimensiune)
	{
		poz=0;
		lungimeMaxima=dimensiune;
		multime=new int[dimensiune];
	}

	private void setMultime(int[] m)
	{
		int n=m.length;
	    for(int i=0;i<n;i++)
	    {
	    	this.multime[i]=m[i];
	    }
	    
	}
	public void adauga(int elem)//pt. adaugarea unui element dat ca parametru in multime
	{
		if(poz<lungimeMaxima)
		{
		multime[poz]=elem;
		poz=poz+1;
		}
		else
		{
			System.err.println("[Error]The set is full, no more elements can be accepted "+elem);
		}
		
	}
	public void sterge(int elem)//pt. eliminarea unui element dat ca parametru din multime
	{
		int i=0;
		int[] aux=new int[lungimeMaxima];
		int pozAux=0;
		int nrAparitii=0;
		while(i<poz)
		{
			if(this.multime[i]!=elem)
			{
			  	aux[pozAux]=this.multime[i];//am folosit un vect. aux. pt. eliminarea elem
			  	pozAux++;
			}
			else
			{
				nrAparitii++;
			}
			i++;
		}
		this.setMultime(aux);
		poz=poz-nrAparitii;
		
	}
	
	private boolean apartine(int []m, int elem)//verifica daca un element apartine unei multimi
	{
		int i=0;
		boolean gasit=false;
		while((i<m.length)&&(gasit==false))
		{
			if(m[i]==elem)
			{
				gasit=true;
			}
			else
			{
				i++;
			}
		}
		return gasit;
		
	}
	public void reuniune(int[] m)//operatia de reuniune a doua multimi
	{
		int aux[]=new int[lungimeMaxima];
		int i=0;
		for(i=0;i<poz;i++)
		{
			aux[i]=this.multime[i];
		}
		int nrEl=0;//numarul de elemente noi gasite
		for(int j=0;j<m.length;j++)
		{
		   boolean gasit=this.apartine(aux,m[j]);
		   if(gasit==false)//s-a gasit un nou element, care nu apare in multimea initiala
		   {
			   aux[i]=m[j];
			   i++;
			   nrEl++;
		   }
			   
		}
		this.setMultime(aux);
		poz=poz+nrEl;
	}
	public void afiseaza()
	{
		if(poz>0)
		{
		System.out.print("The Set:{");
		for(int i=0;i<poz-1;i++)
		{
			System.out.print(this.multime[i]+",");
		}
		System.out.println(this.multime[poz-1]+"}");
		}
		else
		{
			System.out.println("The set is null");
		}
	}

}
